/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package game_shooter;

import framework.Background;
import framework.GameObjectSprite;
import framework.Key;
import framework.Map;
import framework.MapObjectsInitializer;
import framework.ObjectsCollection;
import framework.Screen;
import framework.Sound;
import framework.Text;
import framework.Util;
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import main.Global;

/**
 *
 * @author marcos
 */
public class GameScreenShooter_3 extends Screen implements MapObjectsInitializer {

    private static final int STATE_TITLE = 0;
    private static final int STATE_PLAY = 1;
    private static final int STATE_GAMEOVER = 2;
    private int gameState;
    private Background background;
    private Ship ship;
    private MissleLauncher missleLauncher;
    private MissleLauncher missleLauncher2;
    private MissleLauncher missleLauncher3;
    private Map map;
    private ObjectsCollection enemies;
    private Random random;
    private int timetobullet;

    public void loadResources() {
        background = new Background(Util.loadImage(Global.CIS_FUNDO_LEVEL3),
                Background.SCROLL_NONE);

        ship = new Ship(Util.loadImage(Global.CIS_NAVE_JOGADOR), 30, 40);


       // enemies = new ObjectsCollection(20);

        Image imageMissle = Util.loadImage(Global.EVADER_IMG_MISSLE);

        missleLauncher = new MissleLauncher(imageMissle, 14, 4, Global.BULLET_FIRE_FELAY, Global.BULLET_MAX_NUMBER);

        random = new Random();

    }

    public void initGame() {
        gameState = STATE_TITLE;

        map = Map.getInstance();
        map.reset();
        map.setMapFileTmx(Global.CIS_MAPA_LEVEL3);
        map.setMapTilesImage(Global.CIS_MAPA_TILES);
        map.setTiledLayerTmx();
        map.extractGameObjectsTmx(this);

        map.addObjectsCollection(missleLauncher);
        missleLauncher.makeAllObjectsAvailable();

        //System.out.println("Tamanho do missleLauncher"+missleLauncher.getSize()+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//        missleLauncher2.makeAllObjectsAvailable();
//        missleLauncher3.makeAllObjectsAvailable();

        ship.setX((map.getMapWidth() - ship.getWidth()) / 2);
        ship.setY(map.getMapHeight() - ship.getHeight() * 2);
        ship.setSpeedY(0);

        map.setViewX(ship.getX());
        map.setViewY(ship.getY());
        Sound.play(Global.CIS_song_LEVEL3, true);

    }

    public GameObjectSprite mapObjectFound(int tileIndex) {
        GameObjectSprite obj = null;

        switch (tileIndex) {
            case 1:
                obj = ship;
                break;
            case 2:
                obj = new Enemy(Util.loadImage(Global.CIS_NAVE_INIMIGA_LEVEL3), 30, 40);
                enemies.addObject(obj);
                break;
            case 3:
                obj = new Boss(Util.loadImage(Global.CIS_BOSS_LEVEL3A));
                break;

        }
        return obj;
    }

    public void update() {
        switch (gameState) {
            case STATE_TITLE:
                updateTitle();
                break;
            case STATE_PLAY:
                updatePlay();
                break;
            case STATE_GAMEOVER:
                updateGameOver();
                break;
        }
    }

    private void updateTitle() {
        if (Key.FIRE) {
            gameState = STATE_PLAY;
        }
    }

    private void updatePlay() {
        background.update();
        ship.update();
        enemies.updateActiveObjects();


        if (Key.FIRE) {
            System.out.println("atirou");
            // missleLauncher.createBullet(imageMissle, 14, 4, missleLauncher);
            missleLauncher.fire(ship.getX(), ship.getY(),
                    90, Global.BULLET_SPEED);



        }
        missleLauncher.update();

        for (int i = 0; i < 100; i++) {
            missleLauncher.getObject(i).setVisible(true);
        }
        timetobullet++;
        missleLauncher.updateActiveObjects();


        if (map.getViewY() >= (map.getMapHeight() - map.getViewHeight())) {
            ship.setSpeedY(-3);
        }
        if (map.followsAndCollides(ship)) {
            gameState = STATE_GAMEOVER;
        }
        if (enemies.collidesWithActiveObjects(ship, true)) {
            gameState = STATE_GAMEOVER;
        }

    }

    private void updateGameOver() {
        if (Key.FIRE) {
            gameState = STATE_TITLE;

            map.reset();
            enemies.removeAll();
            initGame();
        }
    }

    public void paint(Graphics g) {
        switch (gameState) {
            case STATE_TITLE:
                paintTitle(g);
                break;
            case STATE_PLAY:
                paintPlay(g);
                break;
            case STATE_GAMEOVER:
                paintGameOver(g);
                break;
        }
    }

    private void paintTitle(Graphics g) {
        Util.clearScreen(g, 0xFFFFFF);
        Text.drawText("CROSSROADS IN SPACE", Text.CENTER, 100, g);
        Text.drawText("PRESS FIRE TO PLAY", Text.CENTER, 200, g);
    }

    private void paintPlay(Graphics g) {
        background.paint(g);
        map.paint(g);
        missleLauncher.paint(g);
    }

    private void paintGameOver(Graphics g) {
        Util.clearScreen(g, 0xFFFFFF);
        Text.drawText("G A M E   O V E R", Text.CENTER, 100, g);
        Text.drawText("PRESS FIRE TO PLAY", Text.CENTER, 200, g);
    }
}
